package com.agileai.pm.module.base.handler;

import java.util.List;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.module.base.service.ProjectReportManage;
import com.agileai.util.ListUtil;

public class ProjectReportManageEditHandler
        extends StandardEditHandler {
    public ProjectReportManageEditHandler() {
        super();
        this.listHandlerClass = ProjectReportManageListHandler.class;
        this.serviceId = buildServiceId(ProjectReportManage.class);
    }
    private static String defDuplicateMsg = "当前汇报人员邮箱已存在，请检查！";
    public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		if (isReqRecordOperaType(operaType)){
			DataRow record = getService().getRecord(param);
			this.setAttributes(record);
			param.put("PR_NAME", record.stringValue("PR_NAME"));
			param.put("PR_POST", record.stringValue("PR_POST"));
			param.put("PR_SORT", record.stringValue("PR_SORT"));
		}
		this.setAttributes(param);
		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
    
    public ViewRenderer doSaveAction(DataParam param){
		String operateType = param.get(OperaType.KEY);
		if (OperaType.CREATE.equals(operateType)){
			getService().createRecord(param);	
		}
		else if (OperaType.UPDATE.equals(operateType)){
			getService().updateRecord(param);	
		}
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
    @PageAction
	public ViewRenderer checkUnique(DataParam param){
		String responseText = "";
		String operateType = param.get(OperaType.KEY);
		String piId = param.get("PI_ID");
		if (OperaType.CREATE.equals(operateType)){
			List<DataRow> records = getService().findPrMailRecords(piId,"");
			String prMail = param.get("PR_MAIL");
			if (!ListUtil.isNullOrEmpty(records)){
				for (int i = 0; i < records.size(); i++) {
					DataRow row = records.get(i);
					if(prMail.equals(row.getString("PR_MAIL"))){
						responseText = defDuplicateMsg;
					}
				}
			}
		}
		if (OperaType.UPDATE.equals(operateType)){
			String prId = param.get("PR_ID");
			String prMail = param.get("PR_MAIL");
			List<DataRow> records = getService().findPrMailRecords(piId,prMail);				
			if (!ListUtil.isNullOrEmpty(records)&&!prId.equals(records.get(0).getString("PR_ID"))){
				responseText = defDuplicateMsg;
			}
		}	
		return new AjaxRenderer(responseText);
	}
	
    protected void processPageAttributes(DataParam param) {
        setAttribute("PR_SEX",FormSelectFactory.create("USER_SEX").addSelectedValue(getOperaAttributeValue("PR_SEX","M")));
    }

    protected ProjectReportManage getService() {
        return (ProjectReportManage) this.lookupService(this.getServiceId());
    }
}
